// import axios from 'axios'
const axios = require('axios')

let centerApi = axios.create({
    baseURL: 'http://192.168.1.112:8282/center/',
    withCredentials: true
});


//请求 ping 接口，如果返回用户数据说明已经登录
centerApi.get("ping").then(response => {
    let loginUser = response.data
    axios.get('http://192.168.1.112:8282/newhiclass/',{withCredentials: true})
    console.log("用户数据", loginUser)
}).catch(err => {
    console.log(">>>>> 需要登录 <<<<<")
    if(confirm("是否登录")){

        getTGT().then(res => {
            return getST(res.data)
        }).then(res => {
            return centerApi.get('/login/cas?ticket='+res.data)
        }).then(res=>{
            
            return centerApi.get('ping')
        }).then(res=>{
            axios.get('http://192.168.1.112:8282/newhiclass/',{withCredentials: true})
            console.log(">>>>> 登录成功 <<<<<",res.data)
        })

    }
})


function getTGT() {
    
    var params = new URLSearchParams();
    params.append('username', 'test1');
    params.append('password', '1234567');
    params.append('global', 'true');
    
    console.log(">>>>> 账号密码登录 <<<<<",{username:params.get('username'),password:params.get('password')})

    //Using application/x-www-form-urlencoded format
    return axios.post("http://192.168.1.112:8282/cas/v1/tickets",params,{withCredentials: true})
}

function getST(tgt) {

    console.log(">>>>> 获取ST <<<<<")

    return axios.post("http://192.168.1.112:8282/cas/v1/tickets/" + tgt+"?service=test")
}
